package com.myproject.music.controller;

import com.myproject.music.BizException;
import com.myproject.music.domain.Comment;
import com.myproject.music.service.CommentService;
import com.myproject.music.util.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/comment")
@Api(tags = "评论管理0515郭彦良")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @ApiOperation("查询评论列表")
    @ApiImplicitParam(name="name", value="歌手", dataType="string", dataTypeClass=String.class)
    @GetMapping("/list")
    public AjaxResult list(@RequestParam(value = "name", required = false) String creator) {
        List<Comment> list = commentService.selectCommentByParam(creator);
        return AjaxResult.success(list);
    }

    @ApiOperation(value="获取评论详细信息", notes = "根据评论ID获取详细信息")
    @ApiImplicitParam(name = "id", value="评论ID", required=true, dataType = "long", paramType="path", dataTypeClass=Long.class)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        Comment comment=commentService.selectCommentById(id);
        if(comment ==null||comment.getId()==null){
            throw new BizException(404,"评论"+id+"不存在");
        }
        return AjaxResult.success(comment);
    }
    // @Log(title = "歌手管理",businessType = BusinessType.INSERT)

    @ApiOperation("新增评论")

    @PostMapping("/insertcomment")
    public AjaxResult add( @RequestBody Comment comment) {
        return AjaxResult.success(commentService.insertComment(comment));
    }

    @ApiOperation("修改评论")
    @PutMapping
    public AjaxResult edit(@RequestBody Comment comment) {
        return AjaxResult.success(commentService.updateComment(comment));
    }

    @ApiOperation("删除评论")
    @ApiImplicitParam(name="id", value = "评论ID", required = true, dataType = "long", paramType = "path", dataTypeClass = Long.class)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return AjaxResult.success(commentService.deleteCommentById(id));
    }

    @ApiOperation("查询全部评论列表")
    @GetMapping("/commentlist")
    public AjaxResult getAllSongList() {
        return AjaxResult.success(commentService.selectCommentAll());
    }
}
